<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/**
 * CRON to create the moving average table (for non boolean modules): 15 minute MA
 * @author Duleepa Wijayawardhana <dups@bioware.com>
 * @version Thu Oct 19 11:59:00 2006
 */
require("/bigdaddy/phplib/init/cron_init.inc");
set_time_limit(0);
/**
 * Constant for the correct moving average
 */
define("MA_TYPE", "nonbool");
/**
 * Constant for the database table
 */
define("MA_TABLE", "nonbool_server_module_history");
/**
 * Increment for the moving average in minutes
 */
define("MA_INCREMENT_MINUTE", 60);
/**
 * CronID
 */
define("CRON_ID", 7);
cronstart(CRON_ID. "CRON_NO_DUPLICATE_PROCESS");

//Set the wait time out on the original connection as we seem to have some issues
$sql = "SET WAIT_TIMEOUT=28800";
db_query($sql);

//When did I last end?
$sql = "SELECT last_end
    FROM ma_cronstatus
    WHERE ma_type = '".MA_TYPE."'";
$aResult = db_fetch_result(db_query($sql, array("dbHandle" => "bigdaddystats")));

$nLastEnd = $aResult["last_end"];

if ($nLastEnd < 10000)
{
    $nLastEnd = mktime(0,0,0,10,25,2006);
}

//Set up the time for now.
$nNow = time();

$nDoTime = $nLastEnd;

$sql = "SELECT sm.ID, sm.serverID, sm.moduleID
        FROM server_module sm
        LEFT JOIN module m ON m.ID = sm.moduleID
        WHERE m.type = 2";
$aModules = db_fetch_array(db_query($sql));

//var_dump($aModules);

while ($nDoTime < $nNow - (MA_INCREMENT_MINUTE * 60))
{
    $nMaxDoTime = $nDoTime + 4000;
    $aCurrentDate = getdate($nMaxDoTime);
    $nCurrentStamp = mktime($aCurrentDate["hours"], 0, 0, $aCurrentDate["mon"], $aCurrentDate["mday"], $aCurrentDate["year"]);
    $nNextStamp = $nCurrentStamp+3599;
        
    foreach ($aModules as $aModule)
    {
        $sql = "DELETE FROM ".MA_TABLE." WHERE
                serverModuleID = '".$aModule["ID"]."' AND
                monHourStamp = '".$nCurrentStamp."'";
        db_query($sql, array("dbHandle" => "bigdaddystats"));
        
        $sql = "SELECT *
            FROM server_module_history
            WHERE serverModuleID = ".$aModule["ID"]."
                AND mon_lastreport BETWEEN $nCurrentStamp AND $nNextStamp
                ORDER BY mon_lastreport ASC";
            //echo $sql;
        $aResult = db_fetch_array(db_query($sql, array("dbHandle" => "bigdaddystats")));
        
        if (is_array($aResult))
        {
            foreach($aResult as $aRow)
            {
                $aData[] = ($aRow["mon_lastreport"] - $nCurrentStamp).":".$aRow["lastreport"];
            }
            
            $sData = join($aData, ";");
                        
            $sql = "INSERT INTO ".MA_TABLE." SET
                lastreportData = '".db_escape_string($sData)."',
                monHourStamp = '".$nCurrentStamp."',
                serverID = '".$aModule["serverID"]."',
                serverModuleID = ".$aModule["ID"].",
                moduleID = '".$aModule["moduleID"]."'";
            //echo $sql;
            db_query($sql, array("dbHandle" => "bigdaddystats"));
            
            unset($aData);
            unset($sData);
        }
    }
    
    //No set the last end time
    $sql = "UPDATE ma_cronstatus SET
        last_end = $nCurrentStamp
        WHERE ma_type = '".MA_TYPE."'";
    db_query($sql, array("dbHandle" => "bigdaddystats"));
    
    $nDoTime = $nNextStamp;
}


cronend(CRON_ID);


?>
